﻿var uploadpath = GetRootPath() + "Resource/component/upload/template.html";
Vue.component('common-upload', function (resolve, reject) {
    $.get(uploadpath).then(function (res) {
        resolve({
            template: res,
            props: {
                groupguid: {
                    default: ''
                },
                grouptype: {
                    default: ''
                },
                uploadTipText: {
                    default: ''
                },
                uploadButtonText: {
                    default: '点击上传'
                },
                uploadWidth: {
                    default: 360
                },
                readonly: {
                    default: false
                },
                controltype: {
                    //upload 附件 image 图片 import 导入文件
                    default: 'upload'
                },
                imagetype: {
                    default: 'single'
                },
                importUrl:{
                    default: ''
                },
                importData:{
                    default:()=>{}
                },
                importButtonnName:{
                    default: ''
                }
            },
            data() {
                return {
                    imageUrl: '',
                    uploadUrl: GetRootPath() + "Frame/Attach/Upload",
                    deleteGroupUrl: GetRootPath() + "Frame/Attach/DeleteGroup",
                    deleteUrl: GetRootPath() + "Frame/Attach/Delete",
                    fileListUrl: GetRootPath() + "Frame/Attach/List",
                    uploadData: {
                        GroupGuid: this.groupguid,
                        GroupType: this.grouptype
                    },
                    fileList: [],
                };
            },
            watch: {
                groupguid:function(val){
                    this.bindFileList();
                }
            },
            mounted() {
                this.bindFileList();
            },
            methods: {
                beforeUpload: function (file) {
                    if (this.controltype == 'image' && this.imagetype == 'single') {
                        var data = {
                            GroupGuid: this.groupguid,
                            GroupType: this.grouptype
                        }
                        var page = this;
                        JsonAjaxPost(this.deleteGroupUrl, JSON.stringify(data), function (result, status) {
                            if (result.code == 0) {
                                console.log("头像删除成功")
                            } else {
                                console.log("头像删除失败")
                            }
                        })
                    }
                    return true;
                },
                //文件点击事件
                handlePreview(file) {
                    window.top.open(file.url)
                },
                //如果是多选，超过选择文件数量
                handleExced(files, fileList) {

                },
                //上传文件成功事件
                handleSuccess(res, file) {
                    if (res.code == 0) {
                        console.log("文件上传成功")
                    } else {
                        console.log("文件上传失败")
                    }
                    if(this.controltype=='import'){
                        this.$emit("import-complate",res);
                    }
                    this.bindFileList();
                },
                //上传头像成功事件
                handleSingleImgSuccess(res, file) {
                    if (res.code == 0) {
                        console.log("头像上传成功")
                    } else {
                        console.log("头像上传失败")
                    }
                    this.bindFileList();
                },
                beforeRemove(file, fileList) {
                    return true;
                },
                //文件删除事件
                handlerRemove(file, fileList) {
                    console.log(file)
                    var data = {
                        RowGuid: file.RowGuid
                    }
                    var page = this;
                    JsonAjaxPost(this.deleteUrl, JSON.stringify(data), function (result, status) {
                        if (result.code == 0) {
                            console.log("文件删除成功")
                        } else {
                            console.log("文件删除失败")
                        }
                        page.bindFileList();
                    })
                },
                //绑定文件列表
                bindFileList() {
                    if (this.controltype == 'upload') {
                        var data = {
                            GroupGuid: this.groupguid,
                            GroupType: this.grouptype
                        }
                        var page = this;
                        JsonAjaxPost(this.fileListUrl, JSON.stringify(data), function (result, status) {
                            if (result.code == 0) {
                                result.FileList.forEach(element => {
                                    element.name = element.FileName
                                    element.url = GetRootPath() + "Frame/Attach/ReadFile?RowGuid=" + element.RowGuid;
                                });
                                Vue.set(page, "fileList", result.FileList);
                            }
                        })
                    } else if (this.controltype == 'image' && this.imagetype == 'single') {
                        var data = {
                            GroupGuid: this.groupguid,
                            GroupType: this.grouptype
                        }
                        var page = this;
                        JsonAjaxPost(this.fileListUrl, JSON.stringify(data), function (result, status) {
                            if (result.code == 0) {
                                if(result.FileList.length>0){
                                    page.imageUrl = GetRootPath() + "Frame/Attach/ReadFile?RowGuid=" + result.FileList[0].RowGuid;
                                }
                            }
                        })
                    }
                }
            },

        })
    });
});
